<template>
  <div class="mailWrap">
    <!-- 邮箱登录 -->
    <div class="weui-cell">
      <div class="weui-cell__hd">
        <label class="weui-label">邮箱</label>
      </div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="email" v-model="email" placeholder="请输入邮箱号">
      </div>
    </div>
    <!-- 按钮 -->
    <a href="javascript:;" class="weui-btn weui-btn_primary" @click="submit">确定</a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      email: "",
      type: 0
    };
  },
  methods: {
    submit() {
      var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(
        this.email
      );
      console.log(this.email);
      console.log(reg);
      console.log(this.type);
      let that = this;
      if (reg) {
        if (this.type == 1) {
          console.log("type1");
          this.$axios
            .post(`${this.$url}/drive/bind?email=${this.email}`)
            .then(res => {
              console.log(res);
              // 成功回调
              if (res.data.IsSuccess) {
                that.$router.push("/driveSuccess");
              } else {
                weui.alert(res.data.Msg);
              }
            })
            .catch(err => {
              // weui.alert('绑定失败，请联系管理员')
            });
        } else {
          console.log("type2");
          this.$axios
            .post(`${this.$url}/printer/bind?email=${this.email}`)
            .then(res => {
              console.log(res);
              // 成功回调
              if (res.data.IsSuccess) {
                that.$router.push("/");
              } else {
                weui.alert(res.data.Msg);
              }
            })
            .catch(err => {
              // weui.alert('绑定失败')
            });
        }
      } else {
        weui.topTips("请输入正确的邮箱");
      }
    }
  },
  created() {
    this.type = this.$route.query.type || 0;
  }
};
</script>

<style lang='less' scoped>
.mailWrap {
  text-align: left;
  padding-top: 80px;
  font-size: 17px;
}

.weui-btn {
  margin: 0 15px;
  margin-top: 60px; // text-decoration: none;
}
</style>
<style lang="css">
.weui-btn_primary {
  background-color: #19a4ff;
  text-decoration: none;
}

.weui-btn_primary:not(.weui-btn_disabled):active {
  background-color: #19a4ff;
}
</style>
